import requests
import time
import js2py
import random
import re
from urllib import parse


class BossSpider(object):
    def __init__(self,url):
        self.url = url
        self.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"
        # self.session = requests.session()
        
    def seedRequest(self, url, header, data=None):
        response = requests.get(url, headers=header, data=data)  # verify=False
        response.encoding = "utf8"
        print(response.status_code," ->", url)
        return response
    
    def getFirstCookie(self):
        response = self.seedRequest(self.url, header={"user-agent": self.userAgent})
        self.firstUrl = response.url
        search = self.firstUrl.split("?")[1].split("&")
        args = [i.split("=")[1] for i in search]
        self.seed, self.name, self.ts, self.callback = parse.unquote(args[0]), args[1], args[2],args[3]
        self.cookieArg = {
            "__c": self.ts[:-3],
            "__g": "-",
            "__a": "%d.%s..%s.1.1.1.1"%(int(100000000*random.random()), self.ts[:-3], self.ts[:-3]),
            "__l": "l=" + parse.quote(self.firstUrl[7:])+"&r=&friend_source=0",
        }
        print("seed = ", self.seed)
        print("  ts = ", self.ts)
        return self.cookieArg
    
    def downloadJs(self):
        header = {
            "user-agent": self.userAgent,
            "cookie": "__c={__c}; __g={__g}; __l={__l}; __a={__a}".format(**self.getFirstCookie()),
            "referer": self.firstUrl
        }
        
        url = "https://www.zhipin.com/web/common/security-js/" + self.name + ".js"
        responseJs = self.seedRequest(url, header)
#         with open("./002.js", "w", encoding="utf8") as fp:
#             fp.write(responseJs.text)
#             print("------文件写入完毕002----------")
            
        jsText = re.sub('setInterval\(function\(\).*?0x1f4\);', "", responseJs.text, flags=re.S)
        
        jsText = 'function code(seed, ts){ var window =  {"navigator": {"userAgent" : "'+ self.userAgent +'","webdriver" : undefined,"appVersion": "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"},"screen": {"availHeight": 864,"availLeft": 0,"availTop": 0,"availWidth": 1536,"colorDepth": 24,"height": 864,"pixelDepth": 24,"width": 1536},"callPhantom": undefined,"_phantom": undefined,"outerWidth": 1536,"outerHeight": 864,"innerWidth": 0,"innerHeight": 0,"moveBy": function () {return undefined;},"moveTo": function() {return ""},"open": function(){return "";},"dispatchEvent": function (e) {return null;}};var document = {"getElementById": function (str) {return "I do not know what has been returned";},"title": "","createElement": function (flag) {return "<"+flag+"></"+flag+">";}};var top = { "location": { "href": "https://www.zhipin.com/web/common/security-check.html"}};' + jsText + "return new ABC().z(seed, parseInt(ts)+(480+new Date().getTimezoneOffset())*60*1000);}"
        
        with open("./001.js", "w", encoding="utf8") as fp:
            fp.write(jsText)
            print("------文件写入完毕001----------")
        
        code = js2py.eval_js(jsText)
        token = code(self.seed, self.ts)
        print("__zp_stoken__ = ", token)
        return token
    
    def parseData(self):
        self.cookieArg["__zp_stoken__"] = parse.quote(self.downloadJs())
        header = {
            "authority": "www.zhipin.com",
            "method": "GET",
            "path": "/c101190400-p100901/",
            "scheme": "https",
            "accept": "text/html, application/xhtml+xml,application/xml;q=0.9, image/webp, image/apng, */*;q=0.8,application/signed-exchange;v = b3",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "user-agent": self.userAgent,
            "cookie": "__c={__c}; __g={__g}; __l={__l}; __a={__a};__zp_stoken__={__zp_stoken__}".format(**self.cookieArg),
            "referer": self.firstUrl,
            "upgrade-insecure-requests": "1",
            "Connection": "keep-alive",
        }
        # __c={__c}; __g={__g}; __l={__l}; __a={__a};
        print("--"*30,"\nCOOKIE：", header["cookie"])
        response = self.seedRequest(self.url,header)
#         with open("boss001.html", "wb") as fp:
#             fp.write(response.content)
        if response.url == self.url:
            print("-------------网页请求成功--------------> ", response.url)
        else:
            print("-------------网页请求失败-------------- ", response.url)

if __name__ == '__main__':
    spider = BossSpider(url="https://www.zhipin.com/c101230200-p100901")
    spider.parseData()
    

    